package org.jeecg.modules.demo.mall.yxlevelproductdiscount.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.mall.entity.YxLevelProductDiscount;
import org.jeecg.modules.demo.mall.entity.YxShopProduct;
import org.jeecg.modules.demo.mall.entity.YxStoreProduct;
import org.jeecg.modules.demo.mall.entity.YxSystemUserLevel;
import org.jeecg.modules.demo.mall.yxlevelproductdiscount.mapper.YxLevelProductDiscountMapper;
import org.jeecg.modules.demo.mall.yxlevelproductdiscount.service.IYxLevelProductDiscountService;
import org.jeecg.modules.demo.mall.yxshopproduct.mapper.YxShopProductMapper;
import org.jeecg.modules.demo.mall.yxstoreproduct.mapper.YxStoreProductMapper;
import org.jeecg.modules.demo.mall.yxsystemuserlevel.mapper.YxSystemUserLevelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 商品等级价格表
 * @Author: jeecg-boot
 * @Date: 2023-11-10
 * @Version: V1.0
 */
@Service
public class YxLevelProductDiscountServiceImpl extends ServiceImpl<YxLevelProductDiscountMapper, YxLevelProductDiscount> implements IYxLevelProductDiscountService {
    @Autowired
    private YxShopProductMapper yxShopProductMapper;

    @Autowired
    private YxStoreProductMapper yxStoreProductMapper;
    @Autowired
    private YxSystemUserLevelMapper yxSystemUserLevelMapper;


    @Override
    public YxLevelProductDiscount queryLevelPrice(Integer productType, Integer levelId, Integer MerId, Long productId) {
        LambdaQueryWrapper<YxLevelProductDiscount> queryWrapper = new LambdaQueryWrapper<>();
        //商家商品等级价格查询
        if (productType != null && productType == 1) {
            queryWrapper.eq(YxLevelProductDiscount::getProductType, productType);
            queryWrapper.eq(YxLevelProductDiscount::getMerchantId, MerId);
            queryWrapper.eq(YxLevelProductDiscount::getProductId, productId);
            queryWrapper.eq(YxLevelProductDiscount::getLevelId, levelId);
            return this.getOne(queryWrapper);
        }
        //平台商品等级价格查询
        if (productType != null && productType == 0) {
            queryWrapper.eq(YxLevelProductDiscount::getProductType, productType);
            queryWrapper.eq(YxLevelProductDiscount::getLevelId, levelId);
            queryWrapper.eq(YxLevelProductDiscount::getProductId, productId);
            return this.getOne(queryWrapper);
        }

        return null;
    }

    @Override
    public YxLevelProductDiscount queryName(YxLevelProductDiscount yxLevelProductDiscount) {
        //商家商品等级名称返回
        if (yxLevelProductDiscount.getProductType() != null && yxLevelProductDiscount.getProductType() == 1) {
            YxShopProduct yxShopProduct = yxShopProductMapper.selectById(yxLevelProductDiscount.getProductId());
            if (yxShopProduct == null) {
                yxLevelProductDiscount.setProductName("商品被删除");
            }
            yxLevelProductDiscount.setProductName(yxShopProduct.getStoreName());
        }
        //平台商品等级名称返回
        if (yxLevelProductDiscount.getProductType() != null && yxLevelProductDiscount.getProductType() == 0) {
            YxStoreProduct yxStoreProduct = yxStoreProductMapper.selectById(Integer.valueOf(String.valueOf(yxLevelProductDiscount.getProductId())));
            if (yxStoreProduct == null) {
                yxLevelProductDiscount.setProductName("商品被删除");
            }
            yxLevelProductDiscount.setProductName(yxStoreProduct.getStoreName());
        }
        YxSystemUserLevel levelServiceById = yxSystemUserLevelMapper.selectById(yxLevelProductDiscount.getLevelId());
        yxLevelProductDiscount.setLevelName(levelServiceById.getName());
        return yxLevelProductDiscount;
    }


}
